package com.course.business.controller.admin;

import com.course.server.common.Result;
import com.course.server.dto.ChapterDto;
import com.course.server.query.ChapterPageQuery;
import com.course.server.service.ChapterService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * @author: Durian
 * @date: 2021/4/18 18:27
 */
@Validated
@RestController
@RequestMapping("/admin/chapters")
public class ChapterController {

    /**
     * 用于日志记录使用
     */
    public static final String BUSINESS_NAME = "大章";

    @Resource
    private ChapterService chapterService;

    @GetMapping
    public Result list(@Validated ChapterPageQuery request) {
        return Result.success(chapterService.list(request.getPage(), request.getSize(), request.getCourseId()));
    }

    @PostMapping
    public Result save(@RequestBody @Validated ChapterDto chapterDto) {
        chapterService.save(chapterDto);
        return Result.success();
    }

    @DeleteMapping("{chapterId}")
    public Result delete(@PathVariable("chapterId") @NotBlank(message = "待删除大章ID不能为空") String chapterId) {
        chapterService.delete(chapterId);
        return Result.success();
    }
}
